// ol
import Overlay from "ol/Overlay";
// js
import DrawMethods from './draw.js';
import { transformLonlatToNow, removeDomById } from "./util.js"
import { transfLonAndLat } from "/@/utils/chart"
import { shipType } from "/@/config/shipType.js"
// api
import { api as cameraApi } from '/@/api/plat/camera.js';
import { api as radarApi } from '/@/api/plat/radar.js';
import { api as bridgeApi } from '/@/api/plat/bridge.js';

let cameraList = {}

export default {
  // 绘制摄像头
  drawCamera: (mapTarget) => {
    cameraApi.queryList().then(({ data }) => {
      DrawMethods.drawSign(mapTarget, data, {
        icon: '/images/sign/icon_shexiangtou.png',
        name: 'icon_camera',
        featureType: "CameraPoint"
      });
      for (let item of data) {
        cameraList[item.cameraId] = item;
      }
    });
  },
  drawRadar: (mapTarget) => {
    radarApi.queryList().then(({ data }) => {
      DrawMethods.drawSign(mapTarget, data, {
        icon: '/images/sign/icon_leida.png',
        name: 'icon_radar',
        featureType: "RadarPoint",
        zIndex: 202,
      });
      DrawMethods.drawRadar(mapTarget, data, {
        name: 'icon_radar_circle',
        zIndex: 201,
      })
    });
  },
  // 绘制桥梁
  drawBridgeList: (mapTarget) => {
    bridgeApi.queryList().then(({ data }) => {
      DrawMethods.drawBridgeList(mapTarget, data)
    });
  },
  // 设备的tips
  handleEquipmentPoint: (map, row, params) => {
    removeDomById(params.id)
    const containerDom = document.createElement('div');
    containerDom.setAttribute("id", params.id)
    const dom1 = document.createElement("div");
    dom1.setAttribute("class", "typhoon-info-title")
    dom1.innerHTML = `${row.cameraName || row.radarName}`;
    const dom5 = document.createElement("div");
    dom5.setAttribute("class", "typhoon-info-title")
    dom5.innerHTML = `经度：${transfLonAndLat(row.longitude, "lon", true)}`;
    const dom6 = document.createElement("div");
    dom6.setAttribute("class", "typhoon-info-title")
    dom6.innerHTML = `纬度：${transfLonAndLat(row.latitude, "lat", true)}`;
    // 插入
    containerDom.appendChild(dom1);
    containerDom.appendChild(dom5);
    containerDom.appendChild(dom6);
    let typhoon_info_tips = new Overlay({
      element: containerDom,
      offset: [20, -25],
      positioning: 'center',
      insertFirst: false
    });
    typhoon_info_tips.setPosition(transformLonlatToNow([row.longitude, row.latitude]));
    map.addOverlay(typhoon_info_tips);
  },
  handleShipPoint(map, row, params) {
    function getType(val) {
      const row = shipType.find(m => m.value === val) || { label: "其他" }
      return row.label
    }
    removeDomById(params.id)
    const containerDom = document.createElement('div');
    containerDom.setAttribute("id", params.id)
    const dom1 = document.createElement("div");
    dom1.setAttribute("class", "ship-info-title")
    dom1.innerHTML = `船名：${row.vesselName || '未知'}`;
    const dom6 = document.createElement("div");
    dom6.setAttribute("class", "ship-info-title")
    dom6.innerHTML = `MMSI：${row.mmsi}`;
    const dom7 = document.createElement("div");
    dom7.setAttribute("class", "ship-info-title")
    dom7.innerHTML = `航速：${row.speed}节`;
    const dom8 = document.createElement("div");
    dom8.setAttribute("class", "ship-info-title")
    dom8.innerHTML = `航向：${row.course.toFixed(2)}°`;
    const dom9 = document.createElement("div");
    dom9.setAttribute("class", "ship-info-title")
    dom9.innerHTML = `类型：${getType(row.dataType)}`;
    const dom10 = document.createElement("div");
    dom10.setAttribute("class", "ship-info-title")
    dom10.innerHTML = `经度：${transfLonAndLat(row.longitude, "lon", true)}`;
    const dom11 = document.createElement("div");
    dom11.setAttribute("class", "ship-info-title")
    dom11.innerHTML = `纬度：${transfLonAndLat(row.latitude, "lat", true)}`;
    // 插入
    containerDom.appendChild(dom1);
    containerDom.appendChild(dom6);
    containerDom.appendChild(dom10);
    containerDom.appendChild(dom11);
    containerDom.appendChild(dom7);
    containerDom.appendChild(dom8);
    containerDom.appendChild(dom9);
    let ship_info_tips = new Overlay({
      element: containerDom,
      offset: [20, -25],
      positioning: 'center',
      insertFirst: false
    });
    ship_info_tips.setPosition(transformLonlatToNow([row.longitude, row.latitude]));
    map.addOverlay(ship_info_tips);
  },
  handleMockPoint(map, row, params) {
    removeDomById(params.id)
    const containerDom = document.createElement('div');
    containerDom.setAttribute("id", params.id)
    for (let key in row) {
      const dom1 = document.createElement("div");
      dom1.setAttribute("class", "ship-info-title")
      dom1.innerHTML = `${key}：${row[key]}`;
      // 插入
      containerDom.appendChild(dom1);
    }
    let ship_info_tips = new Overlay({
      element: containerDom,
      offset: [20, -25],
      positioning: 'center',
      insertFirst: false
    });
    ship_info_tips.setPosition(transformLonlatToNow([row.longitude, row.latitude]));
    map.addOverlay(ship_info_tips);
  },
  // 计算夹角
  calculatePanAngle: (lon1, lat1, lon2, lat2) => {
    // 将角度转换为弧度
    const radLat1 = (lat1 * Math.PI) / 180;
    const radLon1 = (lon1 * Math.PI) / 180;
    const radLat2 = (lat2 * Math.PI) / 180;
    const radLon2 = (lon2 * Math.PI) / 180;

    // 计算经度差
    const deltaLon = radLon2 - radLon1;

    // 方位角公式
    const y = Math.sin(deltaLon) * Math.cos(radLat2);
    const x =
      Math.cos(radLat1) * Math.sin(radLat2) -
      Math.sin(radLat1) * Math.cos(radLat2) * Math.cos(deltaLon);
    const panRadians = Math.atan2(y, x);

    // 将弧度转换为角度，并确保在0-360度范围内
    const panDegrees = (panRadians * 180) / Math.PI;
    return (panDegrees + 360) % 360;
  },
  calculateTiltAngle: (distance, height) => {
    // 计算 Tilt 角度
    const tiltRadians = Math.atan2(height, distance);
    // 将弧度转换为角度，并确保在 0-360 度范围内
    const tiltDegrees = (tiltRadians * 180) / Math.PI;
    return (tiltDegrees + 360) % 360;
  },
  // 绘制跟踪摄像头
  drawCameraRange: (mapTarget, row) => {
    const data = {
      pointLongitude: row.longitude,
      pointLatitude: row.latitude,
      cameraLongitude: cameraList[row.cameraId].longitude,
      cameraLatitude: cameraList[row.cameraId].latitude,
    }
    DrawMethods.cameraRange(mapTarget, data, row.cameraId);
  },
  removeCameraRange: (mapTarget, row) => {
    DrawMethods.cameraRange(mapTarget, row.cameraId);
  },
}